package com.blensmile.activity;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

import com.blensmile.BaseActivity;
import com.blensmile.R;
import com.blensmile.adapter.MediaTestAdapter;
import com.blensmile.anims.ExpandAnimation;
import com.blensmile.entities.MediaTestBean;
import com.blensmile.natives.TestNatives;
import com.blensmile.utils.DividerItemDecoration;
import com.blensmile.utils.ImageToast;
import com.blensmile.utils.LogUtils;
import com.blensmile.utils.MediaPlayerHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MediaPlayerTestActivity extends BaseActivity {

    private int listCnt = 2000;
    private List<MediaTestBean> mediaList = new ArrayList<>();
    private MediaTestAdapter mAdapter;
    private RecyclerView rvRecycler;
    private LinearLayout llExpand;
    private ExpandAnimation expandAnim;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1000:
                    handler.sendEmptyMessageDelayed(1000,4000);
                    ImageToast.showSingleToast(String.valueOf(llExpand.isShown()));
                    llExpand.startAnimation(new ExpandAnimation(llExpand,200));
                    break;
                case 2000:
                    handler.sendEmptyMessageDelayed(3000,3000);
                    llExpand.clearAnimation();
                    llExpand.startAnimation(expandAnimation);
                    break;
                case  3000:
                    handler.sendEmptyMessageDelayed(2000,3000);
                    llExpand.clearAnimation();
                    llExpand.startAnimation(collapseAnimation);
                    break;
            }
        }
    };
    private Animation expandAnimation;
    private Animation collapseAnimation;

    class LooperThread extends Thread {
        public Handler mHandler;

        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    sendEmptyMessageDelayed(1,100);
                    LogUtils.b("hello world");

                }
            };
            Looper.loop();
            mHandler.sendEmptyMessage(1);
            LogUtils.b("hello world");
            while(true){}
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player_test);
//        initUserList();
//        initReplyList();
        getList();
        Collections.shuffle(mediaList);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaPlayerHelper.stopAndRelease();
    }


    private void initView(){
        rvRecycler = (RecyclerView) findViewById(R.id.rv_recycler);
        rvRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mAdapter = new MediaTestAdapter(this,mediaList);
        rvRecycler.setAdapter(mAdapter);
        rvRecycler.addItemDecoration(new DividerItemDecoration(this));
        rvRecycler.setHasFixedSize(true);
        rvRecycler.setNestedScrollingEnabled(false);
        llExpand = (LinearLayout)findViewById(R.id.ll_expand);

        expandAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_expand);
        expandAnimation = new TranslateAnimation(0,0f,0,0f,1,-1f,1,0f);
        expandAnimation.setFillAfter(true);
        expandAnimation.setDuration(300);
        collapseAnimation = AnimationUtils.loadAnimation(this,R.anim.anim_collapse);
        collapseAnimation = new TranslateAnimation(0,0f,0,0f,1,0f,1,-1f);
        collapseAnimation.setFillAfter(true);
        collapseAnimation.setDuration(300);
    }

    @Override
    protected void onResume() {
        super.onResume();
        handler.sendEmptyMessage(2000);
        ImageToast.showSingleToast(TestNatives.sayHello(""));
        LooperThread thread =  new LooperThread();
        thread.start();
        LogUtils.b("hello world");
//        thread.mHandler.sendEmptyMessage(100);
    }

    private void initReplyList(){
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,2,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/07/5847fa095d76a.mp3?hash=FrAcH-qisTY88ocVx7U9n9soqUYc"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,2,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/13/584fada54e3e3.mp3?hash=FgvGJ3pKyNs2l7yeFr6OzBVh9wSc"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,3,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/19/58578c7e31004.mp3?hash=Fu4zX2f8a4d216rI8PqPxbQYo2pH"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,4,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e8f2a4f99.mp3?hash=FuJGEC1D-G1L21i2VBBf5Nra1PhO"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,5,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e901ac8f3.mp3?hash=Fh8NTnrKCU8i_HnFCK-jEgl54i-1"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,6,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e9100dfbc.mp3?hash=FoKrpkOMzPIwS9mHsSvCoALT48oG"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,7,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e920d15c1.mp3?hash=FiPv1DQHUVMrgKUHjHoc7Zo8L2Ru"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,8,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e933c2848.mp3?hash=FlnXLVIcjvNkqaGil78Uabp7dfNh"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,9,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e945b28de.mp3?hash=Fo4vZSamEpd5mt40DBPL5Z6tOPPs"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,10,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e9566d5ce.mp3?hash=FnmHPJkB0j2ndODCfhpGzqXtVh0T"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,11,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e9724e7f8.mp3?hash=Ft1YbQJgrmK_c93SRggKx0Y9XiqE"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,12,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e9a3753e4.mp3?hash=FmGnweOQ78SVvstz-IIHA9uZ-W1C"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,13,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e9bb244fa.mp3?hash=FuQ5SyWv8z6czT_J8img_Z9Y8wbf"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,14,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860e9e837af2.mp3?hash=FiONI3xNF984I7cGjGefaHyD7ReQ"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,15,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860ea0160869.mp3?hash=FhB5hfz-QxCTWqSUY3y7vW7lyc66"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,16,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860ea1ba0414.mp3?hash=FpsU2cePANNZO_LgnnbDZq973B7O"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,17,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860ea3b8069b.mp3?hash=FsUKV0LEg6Wkee62zYmiLDvqHVZo"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,18,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860ea569da17.mp3?hash=Flb5ioSKvAIqfgjB_WeOzMG5ydtt"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,19,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860ea72821af.mp3?hash=FrCHJi_WTqvCtXi-3nr4GS9mQfvG"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,20,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/26/5860eaeb6cda0.mp3?hash=Fh325IM5FvZ0RWUFQd5BUI8HuxJ8"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,21,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/27/5861ceba6931c.mp3?hash=Fmq_T-qyPGjbuE-hwARFhU4VIfKR"));
    }

    private void initOfficialList(){
//        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,2,""));
    }


    private void initUserList(){
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,33,"http://uploadfile.talkmate.com/card/sounds/16/06/28/5771fb0709f5a.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,53,"http://uploadfile.talkmate.com/card/sounds/16/08/08/57a87607a50c4.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,98,"http://uploadfile.talkmate.com/card/sounds/16/08/08/57a875c597f18.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,53,"http://uploadfile.talkmate.com/card/sounds/16/08/08/57a876177e0d3.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,20,"http://uploadfile.talkmate.com/card/sounds/16/08/01/579eedc69b2ea.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,20,"http://uploadfile.talkmate.com/card/sounds/16/08/01/579ed66069f03.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,4,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579b1e2a1f91f.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,20,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579afe4b3202b.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,16,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579afaef1b551.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,255,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579af4e2ca373.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,240,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579aeacac2088.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,209,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579ae876d04ec.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,209,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579adf0dcbd3c.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,209,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579adef4064de.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,209,"http://uploadfile.talkmate.com/card/sounds/16/07/29/579adbdb2ebf8.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,20,"http://uploadfile.talkmate.com/card/sounds/16/07/28/5799f2f202df1.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,200,"http://uploadfile.talkmate.com/card/sounds/16/09/29/57ecc2157774b.mp3"));
        mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,226,"http://uploadfile.talkmate.com/card/sounds/16/09/29/57ecc1e5ac783.mp3"));
    }

    private void getList(){
        for(int i = 0;i<listCnt;i++){
            mediaList.add(new MediaTestBean(MediaTestBean.TYPE_USER,226,"http://uploadfile.talkmate.com/card/sounds/16/09/29/57ecc1e5ac783.mp3"));
            mediaList.add(new MediaTestBean(MediaTestBean.TYPE_REPLY,21,"http://uploadfile.talkmate.com/uploadfiles/homework/2016/12/27/5861ceba6931c.mp3?hash=Fmq_T-qyPGjbuE-hwARFhU4VIfKR"));
        }
    }

}
